/*
 * @Author: Siogo 你的邮箱
 * @Date: 2025-11-12 11:40:11
 * @LastEditors: Siogo 你的邮箱
 * @LastEditTime: 2025-11-13 11:09:26
 * @FilePath: /frontend/src/api/service/dataSource.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defHttp } from '/@/utils/http/axios';

export type ServiceSection = 'source' | 'resource' | 'product';

function getServiceUrl(deviceId: string): string {
  return `/api/terminal/${deviceId}/api/service/datasource`;
}

export interface ServiceDataResponse<T> {
  success: boolean;
  data: T[];
}

export interface ServiceDataSource {
  '.name': string;
  '.index': number;
  __index?: number | string;
  data_source_id: string;
  name: string;
  type: 'database' | 'api' | 'txt' | string;
  db_type?: string;
  host?: string;
  port?: string;
  user?: string;
  password?: string;
  database?: string;
  table?: string[] | string;
  remark?: string;
  create_time?: string;
  owner?: string;
}

export interface ServiceDataResource {
  '.name': string;
  '.index': number;
  __index?: number | string;
  resource_name: string;
  resouce_name?: string;
  data_source_id: string;
  data_source_name: string;
  type: string;
  method: string;
  remark: string;
  level?: string | number;
  interval?: string;
  enabled?: string;
  table_name?: string;
  deliver_type?: string;
}

export interface CreateDataSourcePayload {
  data_source_id: string;
  name: string;
  type: 'database' | 'api' | 'txt' | string;
  db_type: 'mysql' | 'postgresql' | 'sqlserver' | string;
  host: string;
  port: string;
  user: string;
  password: string;
  database: string;
  table: string[];
  remark: string;
  create_time: string;
  owner: string;
}

export interface UpdateDataSourcePayload extends CreateDataSourcePayload {
  '.name': string;
  '.index': number | string;
  __index: number | string;
}

export interface CreateDataResourcePayload {
  resource_name: string;
  data_source_name: string;
  data_source_id: string;
  type: string;
  method: string;
  remark?: string;
  level?: string;
  interval?: string;
  enabled?: string;
}

export interface UpdateDataResourcePayload extends CreateDataResourcePayload {
  '.name': string;
  '.index': number | string;
  __index: number | string;
  resouce_name?: string;
}

export function fetchServiceData<T = ServiceDataSource>(deviceId: string, section: ServiceSection) {
  return defHttp.get<ServiceDataResponse<T>>({
    url: getServiceUrl(deviceId),
    params: { section },
  });
}

export function fetchDataResources(deviceId: string) {
  return fetchServiceData<ServiceDataResource>(deviceId, 'resource');
}

export function createDataSource(deviceId: string, data: CreateDataSourcePayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'update', section: 'source' },
    data,
  });
}

export function updateDataSource(deviceId: string, data: UpdateDataSourcePayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'update', section: 'source' },
    data,
  });
}

export function deleteDataSources(deviceId: string, name: string) {
  return defHttp.post({
    url: `${getServiceUrl(deviceId)}?action=delete&section=source&name=${encodeURIComponent(name)}`,
  });
}

export function createDataResource(deviceId: string, data: CreateDataResourcePayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'update', section: 'resource' },
    data,
  });
}

export function updateDataResource(deviceId: string, data: UpdateDataResourcePayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'update', section: 'resource' },
    data,
  });
}

export function deleteDataResource(deviceId: string, name: string) {
  return defHttp.post({
    url: `${getServiceUrl(deviceId)}?action=delete&section=resource&name=${encodeURIComponent(name)}`,
  });
}

export function toggleDataResourceStatus(deviceId: string, name: string, batch: 'enable' | 'disable') {
  const url = `${getServiceUrl(deviceId)}?batch=${batch}&section=resource&name=${encodeURIComponent(name)}&action=update`;
  return defHttp.post({
    url,
  });
}

// 数据产品相关接口定义
export interface ServiceDataProduct {
  '.name': string;
  '.index': number;
  __index?: number | string;
  productName: string;
  industry: string;
  dataSize: string;
  description?: string;
  dataSubject: string | number; // 0=个人信息、1=企业数据、2=公共数据
  providerName: string;
  identityId: string;
  payMethod: string | number; // 0=月付、1=季付、2=年付
  resourceId: string[]; // 资源ID数组
  enabled?: string | number; // 1=上架、0=下架
}

export interface CreateDataProductPayload {
  productName: string;
  industry: string;
  dataSize: string;
  description?: string;
  dataSubject: string;
  providerName: string;
  identityId: string;
  payMethod: string;
  resourceId: string[];
  enabled?: string;
}

export interface UpdateDataProductPayload extends CreateDataProductPayload {
  '.name': string;
  '.index': number | string;
  __index: number | string;
}

// 数据产品 API 函数
export function fetchDataProducts(deviceId: string) {
  return fetchServiceData<ServiceDataProduct>(deviceId, 'product');
}

export function createDataProduct(deviceId: string, data: CreateDataProductPayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'add', section: 'product' },
    data,
  });
}

export function updateDataProduct(deviceId: string, data: UpdateDataProductPayload) {
  return defHttp.postJson({
    url: getServiceUrl(deviceId),
    params: { action: 'update', section: 'product', name: encodeURIComponent(data['.name']), index: data['.index'] },
    data,
  });
}

export function deleteDataProducts(deviceId: string, name: string) {
  return defHttp.post({
    url: `${getServiceUrl(deviceId)}?action=delete&section=product&name=${encodeURIComponent(name)}`,
  });
}

export function toggleDataProductStatus(deviceId: string, name: string, batch: 'enable' | 'disable') {
  const url = `${getServiceUrl(deviceId)}?batch=${batch}&section=product&name=${encodeURIComponent(name)}&action=update`;
  return defHttp.post({ url });
}
